package com.xzwk.subject.application.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xzwk.subject.common.entity.R;
import com.xzwk.subject.domain.convert.SubjectCategoryConverter;
import com.xzwk.subject.domain.dto.SubjectCategoryDTO;
import com.xzwk.subject.domain.entity.SubjectCategoryBO;
import com.xzwk.subject.domain.servcie.SubjectCategoryDomainService;
import com.xzwk.subject.infra.basic.entity.SubjectCategory;
import java.io.Serializable;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 题目分类(SubjectCategory)表控制层
 *
 * @author makejava
 * @since 2024-03-06 21:33:47
 */
@Slf4j
@RestController
@RequestMapping("subjectCategory")
public class SubjectCategoryController extends ApiController {

  /**
   * 服务对象
   */
  @Resource
  private SubjectCategoryDomainService subjectCategoryDomainService;

  @Resource
  private SubjectCategoryConverter subjectCategoryConverter;

  /**
   * 分页查询所有数据
   *
   * @param page            分页对象
   * @param subjectCategory 查询实体
   * @return 所有数据
   */
  @GetMapping("page")
  public R<Object> selectAll(Page<SubjectCategory> page, SubjectCategory subjectCategory) {
    Object page1 = this.subjectCategoryDomainService.page(page,
        new QueryWrapper<>(subjectCategory));
    return R.ok(page1);
  }

  /**
   * 通过主键查询单条数据
   *
   * @param id 主键
   * @return 单条数据
   */
  @GetMapping("{id}")
  public R<SubjectCategoryDTO> selectOne(@PathVariable Serializable id) {
    SubjectCategoryDTO subjectCategoryDTO = this.subjectCategoryDomainService.getById(id);
    return R.ok(subjectCategoryDTO);
  }

  /**
   * 新增数据
   *
   * @param subjectCategoryBO 实体对象
   * @return 新增结果
   */
  @PostMapping("insert")
  public R<Boolean> insert(@RequestBody SubjectCategoryBO subjectCategoryBO) {
    Boolean save = this.subjectCategoryDomainService.save(subjectCategoryBO);
    return R.ok(save);
  }

  /**
   * 修改数据
   *
   * @param subjectCategoryBO 实体对象
   * @return 修改结果
   */
  @PutMapping("update")
  public R update(@RequestBody SubjectCategoryBO subjectCategoryBO) {
    return R.ok(this.subjectCategoryDomainService.updateById(subjectCategoryBO));
  }

  /**
   * 删除数据
   *
   * @param idList 主键结合
   * @return 删除结果
   */
  @DeleteMapping
  public R<Boolean> delete(@RequestParam("idList") List<Long> idList) {
    return R.ok(this.subjectCategoryDomainService.removeByIds(idList));
  }

  /**
   * 查询基本分类列表
   *
   * @return
   */
  @PostMapping("/queryCategory")
  public R<List<SubjectCategoryDTO>> queryCategory(@RequestParam("parentId") Long parentId) {
    List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryPrimaryCategory(parentId);
    List<SubjectCategoryDTO> subjectCategoryDTOList = subjectCategoryConverter.boToSubjectCategoryDTO(
        subjectCategoryBOList);
   return R.ok(subjectCategoryDTOList);
  }
}

